package com.hyfd.deal.Flow;

import java.net.SocketTimeoutException;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.crypto.Data;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.Now;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hyfd.common.GenerateData;
import com.hyfd.common.utils.Base64Utils;
import com.hyfd.common.utils.JsonHelper;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.common.utils.PostUtil;
import com.hyfd.common.utils.PublicRSA;
import com.hyfd.common.utils.Sign;
import com.hyfd.common.utils.ToolDateTime;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.deal.BaseDeal;

/**
 * 郎硕单号码流量充值接口
 * 
 * @author cxj
 *
 */
public class LangShuoFlowDeal implements BaseDeal
{
    Logger log = Logger.getLogger(getClass());
    
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deal(Map<String, Object> order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = -1;
        
        try
        {
            // 手机号
            String number = (String)order.get("phone");
            // 平台订单号
            String orderId = ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS") + number + GenerateData.getIntData(9, 4);
            map.put("orderId", orderId);
            String flowsize = new Double(order.get("value") + "").intValue() + "";
            // 账号
            Map<String, Object> channel = (Map<String, Object>)order.get("channel");// 获取通道参数
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String defaultParameter = (String)channel.get("default_parameter");// 默认参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String username  = paramMap.get("username");// 账户名
            String scope  = paramMap.get("scope");// 账户名
            String reporturl = paramMap.get("reporturl");//回调地址
            String apikey = paramMap.get("apikey");
            long timeStamp = new Date().getTime();
            String method = "GET";
            String sign =MD5.ToMD5("username="+username+"&apikey="+apikey); 
            
    		String params ="?username="+username+"&number="+number+"&flowsize="+flowsize+"&user_order_id="+orderId+"&timestamp="+timeStamp+"&scope="+scope+"&reporturl="+reporturl+"&method="+method+"&sign=" + sign.toUpperCase();
    		
    		String getUrl = linkUrl+params;
    		String retMsg = ToolHttp.get(false, getUrl);
            // 判断是否成功
            if (retMsg != null)
            {
                JSONObject jObject = JSONObject.parseObject(retMsg);
                String resultCode = jObject.getString("errcode");
                String resultMsg = jObject.getString("errmsg");
                String result = jObject.getString("order");
                map.put("resultCode", resultCode + ":" + resultMsg);
                if ("0".equals(resultCode))
                {
                	JSONObject resultObj = JSONObject.parseObject(result);
                	String order_num = resultObj.getString("transaction_id");
                    map.put("providerOrderId", order_num);
                    flag = 1;
                }else{
                	flag = 0;
                }
            }
        }
        catch (ConnectTimeoutException | SocketTimeoutException e)
        {// 请求、响应超时
            flag = -1;
        }
        catch (Exception e)
        {
            log.error("华谷流量出错" + e + MapUtils.toString(order));
        }
        map.put("status", flag);
        return map;
    }
    
}
